/*
 * Copyright 2009 Nominet UK
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * Derivative work produced by Richard Barnes under terms of Apache License
 */

package com.googlecode.ecritdroid;

import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ECRITMain extends Activity {
	private static final String TAG = "ECRITMain";

	private ListView numberTable;
	private ArrayAdapter<String> numberAdapter;
	private Runnable updateTask;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_view);

		ECRITNotificationUtil.updateNotification(getApplicationContext());

		// Wire up the list to its adapter
		numberTable = (ListView) this.findViewById(R.id.number_table);
		numberAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.property_row);
		numberTable.setAdapter(numberAdapter);
		
		// Start the LoST Util and subscribe
		//LoSTService.init(getApplicationContext());
    	Intent svc = new Intent(this, LoSTService.class);
    	startService(svc);
		
		// Subscribe to LoST updates
		updateTask = new Runnable() {
			public void run() {
				updateFromLoSTCache();
			}
		};
		LoSTService.addListener(updateTask);

		// If we don't have mappings, wait for 
		if (!LoSTService.hasMappings()) {
			// Say we're waiting...
			numberAdapter.clear();
			numberAdapter.add(getString(R.string.toast_progress));
		} else {
			updateFromLoSTCache();
		}
	}
	
	public void onDestroy() {
		LoSTService.removeListener(updateTask);
		super.onDestroy();
	}

	public void updateFromLoSTCache() {
		runOnUiThread(new Runnable() {
			public void run() {
				// Display cached numbers in the list
				Set<String> numberSet = LoSTService.getServiceNumbers();

				if (numberSet.size() > 0) {
					numberAdapter.clear();
					Iterator<String> it=numberSet.iterator();
					for (int i=0; it.hasNext(); ++i) {
						numberAdapter.add(it.next());
					}
					numberTable.setOnItemClickListener(new ListSelectListener());
				} else {
					numberAdapter.clear();
					numberAdapter.add(getString(R.string.toast_notfound));
				}
			}
		});
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem m = menu.add(R.string.menu_text_settings);
		m.setIcon(android.R.drawable.ic_menu_preferences);
		m.setIntent(new Intent(getApplicationContext(), ECRITPrefs.class));
		return true;
	}

	private class ListSelectListener implements OnItemClickListener {
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			String number = ((TextView) view).getText().toString();
			Intent newIntent = new Intent(getApplicationContext(), ECRITList.class);
			newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			newIntent.setData(Uri.fromParts("tel", number, null));
			getApplicationContext().startActivity(newIntent);
		}
	}
}
